package firstping.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import firstping.data.QnAPersistenceManager;
import firstping.model.SaveDomain;

@SuppressWarnings("serial")
public class ViewServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		
		String path = ((HttpServletRequest)req).getRequestURI();
		StringTokenizer st = new StringTokenizer( path,"/");
        int count = st.countTokens(); 

        if(count != 2)
        {
        	resp.sendRedirect("/");
        }
        
		// skip one token /question/abcd (remove sites)
		st.nextToken();
		String title_url = st.nextToken();
		
		Query query = psm.newQuery(SaveDomain.class);
		query.setFilter("name=='"+"http://"+title_url+"'");
		query.setOrdering("longDate desc");
		query.setRange(0,15);
		@SuppressWarnings("unchecked")
		List<SaveDomain> list = (List<SaveDomain>)query.execute();
		
		req.setAttribute("list", list);
		req.setAttribute("site", "http://"+title_url);
		
		req.setAttribute("title", "Statistics status for : "+"http://"+title_url);
		req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site,"+title_url);
		req.setAttribute("description", "Statistics status for : "+"http://"+title_url+".We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
		
		try {
			req.getRequestDispatcher("/view.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Change password success");
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		String title_url = req.getParameter("name");
		
		String dayOne = req.getParameter("day_from");
		String timeOne = req.getParameter("time_from");
		String dayTwo = req.getParameter("day_to");
		String timeTwo = req.getParameter("time_to");
		
		req.setAttribute("dayOne", dayOne);
		req.setAttribute("timeOne", timeOne);
		req.setAttribute("dayTwo", dayTwo);
		req.setAttribute("timeTwo", timeTwo);
		
		if(timeOne.equals(""))
		{
			timeOne = "00:00:00";
		}
		if(timeTwo.equals(""))
		{
			timeTwo= "00:00:00";
		}
		String time = dayOne+" "+timeOne;
		String time1 = dayTwo+" "+timeTwo;
		
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd h:m:s");
		Date from = new Date();
		try
		{
			from = (Date)dateformat.parse(time);
		}catch (Exception e)
		{
			
		}
		Date to = new Date();
		try
		{
			to = (Date)dateformat.parse(time1);
		}catch (Exception e)
		{
			
		}

		Query query = psm.newQuery(SaveDomain.class);
		query.setFilter("name=='"+"http://"+title_url+"' && longDate < '"+to.getTime()+"' && longDate > '"+from.getTime()+"'");
		query.setOrdering("longDate desc");
		query.setRange(0,30);
		@SuppressWarnings("unchecked")
		List<SaveDomain> list = (List<SaveDomain>)query.execute();
		
		req.setAttribute("list", list);
		req.setAttribute("site", "http://"+title_url);
		
		req.setAttribute("title", "Statistics status for : "+"http://"+title_url);
		req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site,"+title_url);
		req.setAttribute("description", "Statistics status for : "+"http://"+title_url+".We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
		
		try {
			req.getRequestDispatcher("/view.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
